//
//  BackupViewController.m
//  quickBatchManager
//
//  Created by jack on 15/12/9.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "BackupViewController.h"
#import "NetService.h"
#import "DateUtils.h"
@interface BackupViewController ()

@end

@implementation BackupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)initUI
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *str = [defaults objectForKey:@"backuptime"];
    if([str isValid])
    {
        _backupTimeLabel.text = [NSString stringWithFormat:@"上次备份时间:%@",str];
    }else
    {
        _backupTimeLabel.text = @"上次备份时间:未备份";
    }
   
    [_backupBtn bk_whenTapped:^{
        NetService *service = [[NetService alloc] init];
        NSArray *documents = DOCUMENT_PATH;
        NSString *dbPath = [NSString stringWithFormat:@"%@/db/LKDB.db",[documents objectAtIndex:0]];
        NSFileManager *fileManage = [NSFileManager defaultManager];
       NSData *data = [fileManage contentsAtPath:dbPath];
     //   NSLog(dbPath);
        [self showProgressHUD:@"备份中"];
        [service post:UPLOAD_FILE_URL params:nil filedName:@"file" dataName:@"LKDB" data:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"上传成功");
            [self hideProgressHUD];
            [self showToast:@"备份成功"];
            [defaults setObject:[DateUtils dateToStringByFormat:[NSDate date] format:YYYYMMDDHHMMSS] forKey:@"backuptime"];
            [defaults synchronize];
            [self initUI];
        } fail:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"上传失败%@",[error description]);
            [self hideProgressHUD];
        }];
    }];
    
    
}



@end
